package com.seuic.hnjiningposapp.hnapps.ui.view.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.core.BasePopupView;
import com.seuic.hnjiningposapp.R;
import com.seuic.hnjiningposapp.hnapps.util.FTPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 点钞及设置
 */
public class ChangeFtpPp extends BasePopupView {
    Unbinder bind;
    @BindView(R.id.input_mainkey1)
    EditText inputMainkey1;
    @BindView(R.id.input_change_mainkey1)
    EditText inputChangeMainkey1;
    @BindView(R.id.input_mainkey2)
    EditText inputMainkey2;
    @BindView(R.id.input_change_mainkey2)
    EditText inputChangeMainkey2;
    @BindView(R.id.input_password_gztext)
    Button inputPasswordGztext;
    @BindView(R.id.input_password_outtext)
    Button inputPasswordOuttext;
    onChangeFtpCallBacl mOnChangeMainCallBacl;

    public ChangeFtpPp(@NonNull Context context, onChangeFtpCallBacl onChangeMainCallBacl) {
        super(context);
        this.mOnChangeMainCallBacl = onChangeMainCallBacl;
    }

    @Override
    protected int getPopupLayoutId() {
        return R.layout.change_ftp_pp;
    }

    @OnClick({R.id.input_password_gztext, R.id.input_password_outtext})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.input_password_gztext:
                //确定
                if (inputMainkey1.getText().toString().equals("")) {
                    ToastUtils.showLong("请输入FTPIP");
                    return;
                }
                if (inputChangeMainkey1.getText().toString().equals("")) {
                    ToastUtils.showLong("请输入FTP账户");
                    return;
                }

                if (inputMainkey2.getText().toString().equals("")) {
                    ToastUtils.showLong("请输入FTP密码");
                    return;
                }
                if (inputChangeMainkey2.getText().toString().equals("")) {
                    ToastUtils.showLong("请输入FTP端口");
                    return;
                }
                SPStaticUtils.put("FTPIP", inputMainkey1.getText().toString().trim());
                SPStaticUtils.put("FTPCOMPORT", Integer.parseInt(inputChangeMainkey2.getText().toString().trim()));
                SPStaticUtils.put("FTPUSERNAME", inputChangeMainkey1.getText().toString().trim());
                SPStaticUtils.put("FTPUSEPASSWORD", inputMainkey2.getText().toString().trim());

                FTPUtils.getInstance().initFtpClient(
                        SPStaticUtils.getString("FTPIP"),
                        SPStaticUtils.getInt("FTPCOMPORT"),
                        SPStaticUtils.getString("FTPUSERNAME"),
                        SPStaticUtils.getString("FTPUSEPASSWORD"));
                mOnChangeMainCallBacl
                        .onChangeFtpok();
                break;
            case R.id.input_password_outtext:
                //退出
                mOnChangeMainCallBacl.onChangeFtpout();
                break;
        }
    }

    @SuppressLint("SetTextI18n")
    @Override


    protected void onCreate() {
        super.onCreate();
        bind = ButterKnife.bind(this);
        inputMainkey1.setText(SPStaticUtils.getString("FTPIP"));
        inputChangeMainkey1.setText(SPStaticUtils.getString("FTPUSERNAME"));
            inputMainkey2.setText(SPStaticUtils.getString("FTPUSEPASSWORD"));
        inputChangeMainkey2.setText(SPStaticUtils.getInt("FTPCOMPORT") + "");


    }

    public interface onChangeFtpCallBacl {
        void onChangeFtpok();

        void onChangeFtpout();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
        //注解解绑
        if (bind != null && bind != Unbinder.EMPTY) {
            bind.unbind();
        }
        this.bind = null;
    }
}
